package com.client.clovece;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
/**
 * Each TileButton has its default state. Some are empty, 
 * some are home, some are crossroads where a player starts
 * in the beginning and leaves home at the end. Buttons also have their 
 * current state which means what they display at the moment.
 * Most buttons have the same default and current state most
 * of the time. Current state changes only with the players 
 * moving on the board.
 * */

public class TileButton extends JButton{
	
	private ArrayList <ImageIcon>stateIcons; 
	
	/**
	 * Role tlacitka ve hre.
	 * Container nese podkladovy obrazek a pripadne cislo hrace,
	 * pro ktereho toto tlacitko bude mit vyznam.
	 * (start, cil, krizovatka)
	 * */
	private ButtonStates defaultState;//determines the role of the button in game
	private ButtonStates temporalState = ButtonStates.EMPTY;//determines whether a player stands on this tile
															//Empty means not set == default setting
	
	private int gridArrayId = -1;//index v mrizce grid tridy gamepanel
	private int roleArrayId = -1;//index v poli tridy dane role v gamepanel
	
	public TileButton(){
		//doplnit resize obrazku
		Dimension d = new Dimension(40,40);
		
		setPreferredSize(d);
		setMaximumSize(d);
		setMinimumSize(d);
		initIcons();
		updateUI();
		
	}
	
	private void initIcons(){
		stateIcons = new ArrayList<ImageIcon>(ButtonStates.getSize());
		Configuration c = Configuration.getInstance();
		String[] paths = c.getPaths();
		//stateIcons.add(null);//first icon null to correspond with player id
		for(int i = 0; i < paths.length; i++){
			stateIcons.add(new ImageIcon(paths[i]));
		}
	}
	
	public void setGridArrId(int i){
		gridArrayId = i;
	}
	
	public int getGridArrId(){
		return gridArrayId;
	}
	
	public void setRoleArrId(int i){
		roleArrayId = i;
	}
	
	public int getRoleArrId(){
		return roleArrayId;
	}
	
	public ImageIcon getIconAtIndex(int i){
		return stateIcons.get(i);
	}
	
	public void setDefaultState(ButtonStates role){//former setRole()
		defaultState = role;
	}
	
	
	public ButtonStates getDefaultState(){//former getRole() and getRoleOwner
		return defaultState;
	}
	
	
	public void setTempStateAndIcon(ButtonStates role){//former setRole()
		temporalState = role;
		setIcon(stateIcons.get(role.ordinal()));
	}
	
	public void setTempState(ButtonStates role){//former setRole()
		temporalState = role;
	}
	
	public ButtonStates getTempState(){//former getOccupantId
		if(temporalState != null){
			return temporalState;
		}
		return ButtonStates.EMPTY;
	}
	

	public boolean isTempStateSet(){//former hasOccupant()
		if(temporalState == null || temporalState == ButtonStates.EMPTY)
		return false;
		else return true;
	}	
	
	/*
	public ImageIcon getPlayerIcon(int i){
		switch(i){
			case PL1: return p1homeI;
			case PL2: return p2homeI;
			case PL3: return p3homeI;
			case PL4: return p4homeI;
		}
		return null;
	}
	
	public ImageIcon getHomeOrBoardIcon(int i){
		switch(i){
			case PL1: return p1homeI;
			case PL2: return p2homeI;
			case PL3: return p3homeI;
			case PL4: return p4homeI;
			case NONPLAYABLE: return emptyI;
			case GAME: return roundI;
		}
		return null;
	}
	*/
	public void setIconAndStateDefault(){
		temporalState = ButtonStates.EMPTY;
		setIconDefault();
		
	}
	
	public void setIconDefault(){
		if(defaultState != null){
			setIcon(stateIcons.get((defaultState).ordinal()));
		}
	}
	
	public void setIcon(ButtonStates state){
		setIcon(stateIcons.get((state).ordinal()));
	}
	
	public boolean setIconAndStateShaded(){
		if(isTempBright()){
			temporalState = ButtonStates.getDim(temporalState);
			setIcon(stateIcons.get((temporalState.ordinal())));
			return true;
		}
		return false;
	}
	
	public void setIconAndStateBright(){
		if(isTempDim()){
			temporalState = ButtonStates.getBright(temporalState);
			setIcon(stateIcons.get(temporalState.ordinal()));
		}else{
			System.err.println("Ikona neni ve stavu DIM.");
		}
	}

	public boolean isTempDim(){//former isShaded()
		if(temporalState != null){
			int o = temporalState.ordinal();
			return o >= ButtonStates.PL1DIM.ordinal() &&  o <= ButtonStates.PL4DIM.ordinal() ? true : false;
		}
		return false;
	}

	public boolean isTempBright(){//former isShaded()
		if(temporalState != null){
			int o = temporalState.ordinal();
			return o >= ButtonStates.PL1BRIGHT.ordinal() &&  o <= ButtonStates.PL4BRIGHT.ordinal() ? true : false;
		}
		return false;
	}
	
}
